package com.xiyu.service.repository.office.building;

import com.xiyu.service.model.office.area.OfficeAreaFetcher;
import com.xiyu.service.model.office.building.*;
import com.xiyu.service.vo.office.building.BuildingPageQueryInput;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.Page;

import java.util.Optional;

public interface OfficeBuildingRepository extends JRepository<OfficeBuilding, Long> {
    OfficeBuildingTable officeBuildingTable = OfficeBuildingTable.$;

    default Page<OfficeBuilding> pageQuery(BuildingPageQueryInput inputVO){
        return
            sql().createQuery(officeBuildingTable)
                    .whereIf(inputVO.getAreaId() != null, officeBuildingTable.areaId().eq(inputVO.getAreaId()))
                    .where(officeBuildingTable.name().likeIf(inputVO.getName()))
                    .orderBy(officeBuildingTable.id().desc())
                    .select(officeBuildingTable.fetch(OfficeBuildingFetcher.$.allTableFields().area(OfficeAreaFetcher.$.allTableFields())))
                    .fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());

    }

    default Optional<OfficeBuilding> findDetailById(Long id){
        return sql().createQuery(officeBuildingTable)
                .where(officeBuildingTable.id().eq(id))
                .select(officeBuildingTable.fetch(OfficeBuildingFetcher.$.allTableFields()
                        .buildingFloors(OfficeBuildingFloorFetcher.$.allTableFields())
                        .buildingUnits(OfficeBuildingUnitFetcher.$.allTableFields())))
                .fetchOptional();
    }


}